"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebReqHandler = void 0;
const Config_1 = require("../src/config/Config");
const ErrorCode_1 = require("../src/config/ErrorCode");
const ServerConfig_1 = require("../src/config/ServerConfig");
const DateUtil_1 = require("../src/engine/utils/DateUtil");
const HaoEncrypt_1 = require("../src/engine/utils/HaoEncrypt");
const Logger_1 = require("../src/engine/utils/Logger");
const Md5Helper_1 = require("../src/engine/utils/Md5Helper");
const WebUtil_1 = require("../src/engine/utils/WebUtil");
const ResponseModel_1 = require("../src/model/ResponseModel");
const WebVersionModel_1 = require("./model/WebVersionModel");
const SystemSettingModel_1 = require("../src/model/SystemSettingModel");
const request = require('request');
const crypto = require("crypto");
const multer = require('multer');
const routerMap = {
    updateServerStatus: "/updateServerStatus",
    login_jhao: "/login_jhao",
    get_notice: "/get_notice",
    get_customer: "/get_customer",
    update_system_setting: "/update_system_setting",
};
class WebReqHandler {
    //     private versionStr: string =
    //         `
    //     {
    //         "iv": "1.0.0.100",
    //         "av": "1.0.0.101",
    //         "al": "http://ibirdgame.cn:9604/202109245.apk",
    //         "il": "http://ibirdgame.cn:9604/202109245.apk"
    //     }
    // `
    initVersionConfig() {
        // this.versionStr = HaoEncrypt.encode(this.versionStr);
        WebVersionModel_1.WebVersionModel.initConfig();
    }
    init(app) {
        this.initVersionConfig();
        for (const key in routerMap) {
            app.use(routerMap[key], this.onBaseRoot.bind(this));
        }
    }
    onBaseRoot(req, resp) {
        try {
            let ip = WebUtil_1.WebUtil.getIpByExpressReq(req);
            Logger_1.Logger.log("onBaseRoot=req=", ip, req.baseUrl);
            if (req.baseUrl == routerMap.updateServerStatus) {
                ServerConfig_1.ServerConfig.initConfig(ServerConfig_1.ServerConfig.nowServerConfig.nowPort);
                resp.end("success");
            }
            else if (req.baseUrl == routerMap.login_jhao) {
                this.checkBody(ip, req, resp, this.on_login_jhao.bind(this));
            }
            else if (req.baseUrl == routerMap.get_notice) {
                this.checkBody(ip, req, resp, this.on_get_notice.bind(this));
            }
            else if (req.baseUrl == routerMap.get_customer) {
                this.checkBody(ip, req, resp, this.on_get_customer.bind(this));
            }
            else if (req.baseUrl == routerMap.update_system_setting) {
                SystemSettingModel_1.SystemSettingModel.instance.init();
                resp.end("success");
            }
        }
        catch (error) {
            Logger_1.Logger.warn("onBaseRoot error", error);
        }
    }
    checkBody(ip, req, resp, callback) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body);
                let k = jsonObj["k"];
                let data = jsonObj["data"];
                if (k == Md5Helper_1.Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger_1.Logger.warn("收到非法Http请求 key", ip, req.body);
                    resp.end();
                }
            }
            catch (error) {
                Logger_1.Logger.warn("收到非法Http请求body", ip, req.body, error);
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }
    on_login_jhao(req, resp, data, ip) {
        Logger_1.Logger.log("on_login_jhao=", data, ip);
        if (DateUtil_1.default.now() >= Config_1.Config.serveDate.getTime()) {
            return;
        }
        let channel = data["channel"];
        let v = data["v"];
        let data_resp = {
            "ios_url": WebVersionModel_1.WebVersionModel.ios_download_url,
            "anroid_url": WebVersionModel_1.WebVersionModel.android_download_url,
            'force_update': WebVersionModel_1.WebVersionModel.getForceUpdate(channel, v),
            "need_hotupdate": WebVersionModel_1.WebVersionModel.getNeedUpate(channel, v),
            "[account_server]": ServerConfig_1.ServerConfig.randomAccountServer(),
            // "[log_server]": ServerConfig.randomLogServer(),
            // "[pay_server]": ServerConfig.randomPayServer()
        };
        Logger_1.Logger.log("on_login_jhao resp=", data_resp);
        let data_str = JSON.stringify(data_resp);
        // let obj: object = {
        //     "t": DateUtil.now(),˝
        //     "code": ErrorCode.Code.Suc,
        //     "data": HaoEncrypt.encode(data_str)
        // }
        let respModel = new ResponseModel_1.ResponseModel();
        respModel.code = ErrorCode_1.ErrorCode.Code.Suc,
            respModel.data = HaoEncrypt_1.HaoEncrypt.encode(data_str);
        resp.end(JSON.stringify(respModel));
    }
    on_get_notice(req, resp, data, ip) {
        let respModel = new ResponseModel_1.ResponseModel();
        respModel.data = SystemSettingModel_1.SystemSettingModel.instance.notice;
        resp.end(JSON.stringify(respModel));
    }
    on_get_customer(req, resp, data, ip) {
        let respModel = new ResponseModel_1.ResponseModel();
        respModel.data = SystemSettingModel_1.SystemSettingModel.instance.customer;
        resp.end(JSON.stringify(respModel));
    }
}
exports.WebReqHandler = WebReqHandler;
WebReqHandler.instance = new WebReqHandler();
